package cn.yonyong.base.project.config.solr;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Describtion 获取solr连接工具类
 * @Author yonyong
 * @Date 2019/10/16 16:19
 * @Version 1.0.0
 **/
public class SolrFactory {

    private static SolrClient solrClient = null;

    /**
     * 单例模式-懒汉式（只有用到才会初始化）
     * @return
     */
    public static synchronized SolrClient getClient(){
        if (null == solrClient){
            String url = getProperty();
            HttpSolrClient.Builder builder = new HttpSolrClient.Builder(url);
            solrClient = builder.build();
        }
        return solrClient;
    }

    /**
     * 获取配置文件信息
     * @return
     */
    private static String getProperty() {
        // 获取配置文件
        Properties properties = new Properties();
        InputStream in = SolrFactory.class.getClassLoader().getResourceAsStream("project.properties");
        try {
            properties.load(in);
        } catch (IOException e1) {
            System.out.println("SolrFactory类getUrl方法获取配置文件失败");
        }
        return properties.getProperty("solr.url");
    }

    public static void main(String[] args) {
        SolrClient solrClient = SolrFactory.getClient();
        System.out.println(solrClient);
    }
}
